home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wsc4c21.zip / PAINT.C < prev    next >
C/C++ Source or Header  |  1997-05-22  |  6KB  |  255 lines

  1. /*** paint.c ***/
  2.  
  3. /* BLACK on WHITE [no color] */
  4.  
  5. #include "windows.h"
  6. #include <string.h>
  7. #include "paint.h"
  8. #include "ascii.h"
  9.  
  10. extern HWND hMainWnd;
  11.  
  12. #define MIN(a,b) ((a<=b)?(a):(b))
  13. #define MAX(a,b) ((a>=b)?(a):(b))
  14.  
  15. /* PRIVATE variables */
  16.  
  17. static int TheRow = 0;     /* current row */
  18. static int TheCol = 0;     /* current col */
  19. static int TopRow;
  20. static int LeftCol;
  21. static int RightCol;
  22. static char Buffer[NROWS][NCOLS];  /* display buffer */
  23. static char *RowPtr[NROWS];        /* array of row pointers */
  24. static TEXTMETRIC tm;
  25. static int CharHeight;
  26. static int CharWidth;
  27. static char EndOfLine[1] = {LF};
  28.  
  29. /* PRIVATE functions */
  30.  
  31. void DoTheScroll(void)
  32. {int Row;
  33.  int Col;
  34.  char *Ptr;
  35.  RECT rect;
  36.  /* scroll display buffer */
  37.  TheRow = NROWS-1;
  38.  Ptr = RowPtr[0];
  39.  for(Row=0;Row<NROWS-1;Row++) RowPtr[Row] = RowPtr[Row+1];
  40.  RowPtr[NROWS-1] = Ptr;
  41.  for(Col=0;Col<NCOLS;Col++) *Ptr++ = ' ';
  42.  /* scroll the display */
  43.  ScrollWindow(hMainWnd,0,0-CharHeight,NULL,NULL);
  44.  /* invalidate last row */
  45.  rect.left = 0;
  46.  rect.top  = CharHeight * (NROWS-2);
  47.  rect.right  = CharWidth * (RightCol+1);
  48.  rect.bottom = CharHeight * (NROWS-1);
  49.  InvalidateRect(hMainWnd,&rect,TRUE);
  50.  /* reset boundary */
  51.  TopRow = TheRow;
  52.  LeftCol = TheCol;
  53.  RightCol = TheCol;
  54. } /* end DoTheScroll */
  55.  
  56. static void ClearBuffer(void)
  57. {int Row;
  58.  int Col;
  59.  /* clear screen buffer */
  60.  for(Row=0;Row<NROWS;Row++)
  61.    {for(Col=0;Col<NCOLS;Col++) Buffer[Row][Col] = ' ';
  62.     RowPtr[Row] = &Buffer[Row][0];
  63.    }
  64.  TheRow = 0;
  65.  TheCol = 0;
  66. }
  67.  
  68. /* PUBLIC functions */
  69.  
  70. int PaintGetRowPos(void)
  71. {return (TheRow*CharHeight);
  72. }
  73.  
  74. int PaintGetColPos(void)
  75. {return (TheCol*CharWidth);
  76. }
  77.  
  78. int PaintGetRow(void)
  79. {return TheRow;
  80. }
  81.  
  82. int PaintGetCol(void)
  83. {return TheCol;
  84. }
  85.  
  86. void PaintSetRow(int Row)
  87. {RECT Rect;
  88.  if((unsigned)Row<NROWS)
  89.    {
  90.     Rect.left   = 0;
  91.     Rect.right  = CharWidth * (RightCol+1);
  92.     Rect.top    = CharHeight * MIN(TheRow,Row);
  93.     Rect.bottom = CharHeight * MAX(TheRow+1,Row+1);
  94.     InvalidateRect(hMainWnd,&Rect,TRUE);
  95.     TheRow = Row;
  96.     SetCaretPos(CharWidth*TheCol,CharHeight*TheRow);
  97.    }
  98. }
  99.  
  100. void PaintSetCol(int Col)
  101. {RECT Rect;
  102.  if((unsigned)Col<NCOLS)
  103.    {
  104.     Rect.left   = CharWidth * MIN(TheCol,Col);
  105.     Rect.right  = CharWidth * MAX(TheCol+1,Col+1);
  106.     Rect.top    = 0;
  107.     Rect.bottom = CharHeight * TheRow;
  108.     InvalidateRect(hMainWnd,&Rect,TRUE);
  109.     TheCol = Col;
  110.     SetCaretPos(CharWidth*TheCol,CharHeight*TheRow);
  111.    }
  112. }
  113.  
  114. void PaintInit(void)
  115. {
  116.  HDC hDC;
  117.  hDC = GetDC(hMainWnd);
  118.  SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  119.  GetTextMetrics(hDC,&tm);
  120.  CharHeight = tm.tmHeight + tm.tmExternalLeading;
  121.  CharWidth = tm.tmMaxCharWidth;
  122.  ReleaseDC(hMainWnd,hDC);
  123.  /* initialize screen buffer */
  124.  ClearBuffer();
  125. } /* end InitPaint */
  126.  
  127. void PaintClearScreen(void)
  128. {HBRUSH hBrush;
  129.  RECT   Rect;
  130.  HDC    hDC;
  131.  ClearBuffer();
  132.  hDC = GetDC(hMainWnd);
  133.  hBrush = CreateSolidBrush(RGB(255,255,255));
  134.  SetRect(&Rect, 0,0, NCOLS*CharWidth, NROWS*CharHeight);
  135.  FillRect(hDC,&Rect,hBrush);
  136.  SetCaretPos(0,0);
  137.  InvalidateRect(hMainWnd,&Rect,TRUE);
  138.  ReleaseDC(hMainWnd,hDC);
  139. }
  140.  
  141. void PaintClearEOL(void)
  142. {int    i;
  143.  HBRUSH hBrush;
  144.  RECT   Rect;
  145.  HDC    hDC;
  146.  char   *Ptr;
  147.  Ptr = RowPtr[TheRow] + TheCol;
  148.  for(i=TheCol;i<NCOLS;i++) *(Ptr++) = ' ';
  149.  hDC = GetDC(hMainWnd);
  150.  hBrush = CreateSolidBrush(RGB(255,255,255));
  151.  SetRect(&Rect,
  152.          TheCol*CharWidth, TheRow*CharHeight,
  153.          NCOLS*CharWidth,  (TheRow+1)*CharHeight);
  154.  FillRect(hDC,&Rect,hBrush);
  155.  InvalidateRect(hMainWnd,&Rect,TRUE);
  156.  ReleaseDC(hMainWnd,hDC);
  157. }
  158.  
  159. void PaintMain(HDC hDC,PAINTSTRUCT *ps)
  160. {int Row;
  161.  int FirstRow;
  162.  int FirstCol;
  163.  int NbrRows;
  164.  int NbrCols;
  165.  int ColWidth;
  166.  int X;
  167.  int Y;
  168.  RECT rect;
  169.  /* set colors */
  170.  /* compute row & col stuff */
  171.  FirstRow = ps->rcPaint.top  / CharHeight;
  172.  FirstCol = ps->rcPaint.left / CharWidth;
  173.  NbrRows = (ps->rcPaint.bottom - ps->rcPaint.top)  / CharHeight;
  174.  ColWidth = ps->rcPaint.right  - ps->rcPaint.left;
  175.  NbrCols = MIN(NCOLS,(1+ColWidth) / CharWidth);
  176.  X = ps->rcPaint.left;
  177.  /* consider each row */
  178.  for(Row=FirstRow;Row<FirstRow+NbrRows;Row++)
  179.    {/* paint part of row */
  180.     if((Row>=0)&&(Row<NROWS))
  181.       {/* good row number */
  182.        Y = CharHeight*Row;
  183.        /* compute bounding rectangle */
  184.        rect.left = X;
  185.        rect.top  = Y;
  186.        rect.right  = X + ColWidth;
  187.        rect.bottom = Y + CharHeight;
  188.        /* paint it */
  189.        SetBkMode(hDC,OPAQUE);
  190.        ExtTextOut(hDC,X,Y,ETO_OPAQUE|ETO_CLIPPED,&rect,RowPtr[Row]+FirstCol,NbrCols,NULL);
  191.       }
  192.    }
  193. } /* end PaintMain */
  194.  
  195. void WriteTheString(LPSTR String, int Count)
  196. {int i;
  197.  char TheChar;
  198.  RECT rect;
  199.  TopRow = TheRow;
  200.  LeftCol = TheCol;
  201.  RightCol = TheCol;
  202.  for(i=0;i<Count;i++)
  203.    {TheChar = *String++;
  204.     switch(TheChar)
  205.      {case BS:
  206.         if(TheCol>0)
  207.           {*(RowPtr[TheRow]+TheCol) = ' ';
  208.            TheCol--;
  209.           }
  210.         break;
  211.       case LF:
  212.         /* next line */
  213.         if(++TheRow>=NROWS) DoTheScroll();
  214.         /*break;*/
  215.       case CR:
  216.         TheCol = 0;
  217.         LeftCol = 0;
  218.         break;
  219.       default:
  220.         /* put char into display buffer */
  221.         *(RowPtr[TheRow]+TheCol) = (char)TheChar;
  222.         /* increment 'cursor' */
  223.         if(++TheCol>=NCOLS)
  224.           {/* next line */
  225.            TheCol = 0;
  226.            LeftCol = 0;
  227.            if(++TheRow>=NROWS) DoTheScroll();
  228.           }
  229.         else RightCol++;
  230.         break;
  231.      } /* end switch */
  232.    } /* end for */
  233.  /* compute invalid rectangle */
  234.  if((TopRow!=TheRow)||(LeftCol!=TheCol)||(RightCol!=TheCol))
  235.    {rect.left = CharWidth * LeftCol;
  236.     rect.top  = CharHeight * TopRow;
  237.     rect.right  = CharWidth * (RightCol+1);
  238.     rect.bottom = CharHeight * (TheRow+1);
  239.     InvalidateRect(hMainWnd,&rect,TRUE);
  240.    }
  241. }  /* end WriteTheString */
  242.  
  243. void DisplayLine(LPSTR Ptr)
  244. {WriteTheString(Ptr,lstrlen(Ptr));
  245.  WriteTheString(EndOfLine,1);
  246. } /* end DisplayLine */
  247.  
  248. void DisplayString(LPSTR Ptr)
  249. {WriteTheString(Ptr,lstrlen(Ptr));
  250. }
  251.  
  252. void DisplayChar(char Chr)
  253. {WriteTheString(&Chr,1);
  254. }
  255.